﻿#region Librerias

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using ESF.General;

#endregion


namespace ESF.WinForms.Controls.ImageControls
{
	public class uscImageSheetItem : PictureBox
	{
		#region Eventos y Delegados

		public delegate void MoveToLeft (uscImageSheetItem ppReference, uscImageSheetItem pMove);

		public delegate void MoveRight (uscImageSheetItem ppReference, uscImageSheetItem pMove);
				

		public event MoveToLeft OnMoveLeft;

		public event MoveRight OnMoveRight;
		
		#endregion

	
		#region Constantes

		internal const int HEIGHT = 100;
		internal const int WIDTH = 100;	
		internal const int PADDING = 4;

		public const int DESFACE_X = 6;	

		#endregion


		#region Miembros

		private string _ImageFilename;

		#region Ajuste Click

		private int _MSClick = 0;

		private Timer doubleClickTimer = new Timer();

		private bool _IsFirstClick = true;
        private bool _IsDoubleClick = false;

		#endregion

		#endregion


		#region Propiedades

		public string ImageFilename
		{
			set
			{
				_ImageFilename = value;
				
				Image = ImageHelper.GenerateTumbnail (value, WIDTH, HEIGHT);

				Refresh ();
			}
			get
			{
				return _ImageFilename;
			}
		}

		#endregion
		

		#region Constructor

		public uscImageSheetItem ()
		{	
			Anchor = AnchorStyles.Top;

			Height = HEIGHT;
			Width = WIDTH;
			Margin = new Padding (PADDING);
			
			MouseDown += ControlClick_MouseDown;
			MouseDown += uscImageCarrouselItem_MouseDown;

			AllowDrop = true;

			DragDrop += uscImageCarrouselItem_DragDrop;

			DragEnter += uscImageCarrouselItem_DragEnter;

			ContextMenuStrip = new ContextMenuStrip ();
			
			ContextMenuStrip.Items.Add ("Editar");
			ContextMenuStrip.Items.Add ("Eliminar");


			doubleClickTimer.Interval = 100;
            doubleClickTimer.Tick +=
                new EventHandler(doubleClickTimer_Tick);
		}		


		private void doubleClickTimer_Tick(object sender, EventArgs e)
        {
            _MSClick += 100;
          
            // The timer has reached the double click time limit.
            if (_MSClick >= SystemInformation.DoubleClickTime)
            {
                doubleClickTimer.Stop();

                if (_IsDoubleClick)
                {
                    OnDoubleClick (e);
                }
                else
                {
                    OnClick (e);
                }

                // Allow the MouseDown event handler to process clicks again.
                _IsFirstClick = true;
                _IsDoubleClick = false;
                _MSClick = 0;
            }
        }


		private void ControlClick_MouseDown(object sender, MouseEventArgs e)
        {
            // Verify that the mouse click is in the main hit
            // This is the first mouse click.
            if (_IsFirstClick)
            {
                _IsFirstClick = false;

                // Determine the location and size of the double click 
                // rectangle area to draw around the cursor point.
                Invalidate();

                // Start the double click timer.
                doubleClickTimer.Start();
            }
            // This is the second mouse click.
            else
            {
                // Verify that the mouse click is within the double click
                // rectangle and is within the system-defined double 
                // click period.
                if (_MSClick < SystemInformation.DoubleClickTime)
                {
                    _IsDoubleClick = true;
                }
            }
        }
		
		#endregion

		
		#region Funciones
		
		public int GetBound_X (Control pParent)
		{
			if (pParent == null) { return 0; }

			return pParent.Bounds.X + GetBound_X (pParent.Parent);
		}

		#endregion

		
		#region Eventos

		private void uscImageCarrouselItem_MouseDown(object sender, MouseEventArgs e)
		{
			Parent.DoDragDrop (sender, DragDropEffects.Move);
		}


		private void uscImageCarrouselItem_DragEnter(object sender, DragEventArgs e)
		{
		    if ((e.AllowedEffect & DragDropEffects.Move) != 0 && e.Data.GetDataPresent (typeof(uscImageSheetItem)))
		    {
		        e.Effect = DragDropEffects.Move;
		    }
		}


		private void uscImageCarrouselItem_DragDrop(object sender, DragEventArgs e)
		{
			int intXItem;
			

			intXItem = GetBound_X (this);

			// Esta del lado izquierdo

			if (e.X < (intXItem + uscImageSheetItem.DESFACE_X + (Width / 2)))
			{
				if (OnMoveLeft != null)
				{
					OnMoveLeft (sender as uscImageSheetItem, e.Data.GetData (typeof (uscImageSheetItem)) as uscImageSheetItem);
				}
			}

			// Esta de lado derecho 
			else
			{
				if (OnMoveRight != null)
				{
					OnMoveRight (sender as uscImageSheetItem, e.Data.GetData (typeof (uscImageSheetItem)) as uscImageSheetItem);
				}
			}
		}
		
		#endregion
	}
}